<?php
if(!isset($_SESSION["id_membre"])) {
session_start();
}

if( file_exists("../../lib/mysql_connect.php") )
  require("../../lib/mysql_connect.php");
else
  require("../lib/mysql_connect.php");

/********************************************************
 * Traitement PHP :                                     *
 * Ensemble des fonctions utilisée dans le traitement   *
 * PHP                                                  *
 * *****************************************************/

function get_eleve()
{
  $requete_eleve = "SELECT id_eleve, nom, prenom FROM eleve WHERE
    id_enseignant = '".$_SESSION['id_membre']."' ";

  $requete_eleve_result = mysql_query($requete_eleve);

  if( $requete_eleve_result != FALSE )
  {
    $nb_eleve = mysql_num_rows($requete_eleve_result);
    if( $nb_eleve )
    {
      while($row = mysql_fetch_array($requete_eleve_result)) {
        echo '<OPTION VALUE='.$row['id_eleve'].'>'.$row['nom'].' '.$row['prenom'].'</OPTION>';
      }

    }
    else
      return ;
  }
  else
    return ;
}

function get_name($id_enseignant,$type)
{
  $requete = "SELECT nom, prenom FROM ".$type." WHERE
    id_".$type." = '".$id_enseignant."' ";

  $requete_result = mysql_query($requete);

  if( $requete_result != FALSE )
  {

    $row = mysql_fetch_array($requete_result);

    return $row['nom'].' '.$row['prenom'];

  }
  else
    return ;
}

function get_atelier()
{
  $requete_atelier = "SELECT id_atelier, nom FROM atelier";

  $requete_atelier_result = mysql_query($requete_atelier);

  if( $requete_atelier_result != FALSE )
  {
    $nb_eleve = mysql_num_rows($requete_atelier_result);
    if( $nb_eleve )
    {
      while($row = mysql_fetch_array($requete_atelier_result)) {
        echo '<OPTION VALUE='.$row['id_atelier'].'>'.$row['nom'].'</OPTION>';
      }

    }
    else
      return ;
  }
  else
    return ;
}

function get_bilan_seance_par_eleve_atelier($id_eleve, $id_atelier) 
{
  $requete_atelier = "SELECT SUM(charge * serie * repetition) as value, date FROM exercice_bilan_seance, seance, atelier_bilan_seance
    WHERE id_eleve = '".$id_eleve."'
    AND id_atelier = '".$id_atelier."'
    AND seance.id_seance = atelier_bilan_seance.id_seance
    AND exercice_bilan_seance.id_atelier_bilan_seance = atelier_bilan_seance.id_atelier_bilan_seance
    GROUP BY date
    ";

  $requete_atelier_result = mysql_query($requete_atelier);

  if( $requete_atelier_result != FALSE )
  {
    $nb_eleve = mysql_num_rows($requete_atelier_result);
    if( $nb_eleve )
    {
      $res = array();
      $date = array();
      $value = array();
      while($row = mysql_fetch_array($requete_atelier_result)) { //traitement ici
        array_push($date,$row['date']);
        array_push($value,$row['value']);
      }
      $res = array("date" => $date,"value" => $value);
      return $res;      
    }
    else
      return "";
  }
  else
    return "";
}

/********************************************************
 * Traitement AJAX :                                    *
 * Affichage des ateliers pour un élève sélectionné     *
 * *****************************************************/
 
if( isset($_POST['type']) && $_POST['type'] == "graph")
{
  $type = $_POST['type'];
  $id_atelier = $_POST['id_atelier'];
  $erreur = "";

  if( !isset($id_atelier) || empty($id_atelier) )
    $erreur = "<div class='erreur_div'>Erreur : Champs atelier vide</div>";

  if( $erreur != "" )
    echo $erreur;
  else
  {
    $value = get_bilan_seance_par_eleve_atelier($_SESSION['id_membre'], $id_atelier); 

    if($value == "") {
      echo "<div class='erreur_div'>Erreur : Aucune données disponibles pour cet atelier</div>";
      return;
    }
    else
    {			
      if(count($value['value']) == 1)
      {
        echo "<div class='erreur_div'>Erreur : Une seule donnée disponible pour cet atelier. Graph non générable.</div>";
        return;
      }
      else
      {
        $_SESSION['id_atelier'] =	$id_atelier;
        echo '<img src="actions_eleve/generation_graph.php?'.time().'">'.'<br><br>';         
      }
    }
  } 
}

?>
